package cn.wolfcode.web.controller;

import cn.wolfcode.anno.RequiredPermission;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;

import cn.wolfcode.domain.Role;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.query.JSONResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;

import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller // 标识一下 可以被扫描 创建对象 加入spring容器
@RequestMapping("/employee") // 访问路径的映射地址   作用 ： 可以通过浏览器 进行访问的地址


public class EmployeeController {


    @Autowired// 自动装配  DepartmentService
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired// 自动装配  DepartmentService
    private IRoleService roleService;
    @RequestMapping("/list")
    @RequiredPermission({"员工列表权限","EmployeeController.list"})
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo)
    {  // 返回值是String   返回值的内容 一般是一个地址
        PageInfo pageInfo = employeeService.findAll(qo);
        // 把数据共享给页面
        model.addAttribute("pageInfo", pageInfo);

        List<Department> depts=departmentService.listAll();
        model.addAttribute("depts",depts);
        return "/employee/list";
    }
    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission({"员工删除权限","EmployeeController.delete"})
    public JSONResult delete(Long id) {

            employeeService.deleteById(id);
            return new JSONResult();


    }
    @RequestMapping("/input")
    @RequiredPermission({"员工添加权限","EmployeeController.input"})
    public String input(Long id,Model model) {
        if (id != null) {
            //根据id获取部门信息
            Employee employee=employeeService.findById(id);
            //把数据共享到页面
            model.addAttribute("entity",employee);

        }
        List<Department> depts=departmentService.listAll();
        model.addAttribute("depts",depts);

        List<Role> roles = roleService.listAll();

        model.addAttribute("roles",roles);


        return "/employee/input";
    }
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"员工更新权限","EmployeeController.saveOrUpdate"})
    public String saveOrUpdate(Employee employee,Long[] ids) {
        employeeService.saveOrUpdate(employee,ids);
        return "redirect:/employee/list";
    }//redirect重定向
}
